<template>
  <div class="app-container">
    <el-card>
      <el-form inline :model="searchForm">
        <el-form-item label="分类">
          <el-select v-model="searchForm.tmplCate" placeholder="请选择模板分类" clearable size="small">
            <el-option v-for="item in cateOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="标题">
          <el-input v-model="searchForm.name" placeholder="请输入标题" size="small" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.enableStatus" placeholder="请选择状态" clearable size="small">
            <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="发布平台">
          <el-select v-model="searchForm.platformType" placeholder="请选择发布平台" clearable size="small">
            <el-option v-for="item in platformTypeList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-refresh" @click="resetForm('searchForm')">重置</el-button>
          <el-button size="small" type="primary" icon="el-icon-search" @click="search()">查询</el-button></el-form-item>
      </el-form>

      <el-button icon="el-icon-plus" size="small" type="primary" @click="addHunredQuestion()">新增合同模板</el-button>
      <el-table :data="tableData.data" v-loading="listLoading" border style="width: 100%; margin-top: 20px">
        <el-table-column label="ID" prop="id" align="center" width="55"></el-table-column>
        <el-table-column label="标题" prop="name" align="center"></el-table-column>
        <el-table-column label="分类" prop="platformType" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.dictIndusTmplCateDesc }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="文件" prop="filename" align="center"></el-table-column>

        <el-table-column label="封面图" prop="platformType" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.previewUrl"
              :preview-src-list="[scope.row.previewUrl]"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="发布平台" prop="platformName" align="center">
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enableStatus" :active-value="1" :inactive-value="0"
              @change="editStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="operateTime" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="detail(scope.row.id)">查看</el-button>
            <el-button :disabled="scope.row.enableStatus ==1" size="mini" type="text"
              @click="editHunredQuestion(scope.row)">编辑</el-button>
            <el-button :disabled="scope.row.enableStatus ==1" size="mini" type="text" @click="deleteHunredQuestion(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="pageRight" background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]" :current-page="searchForm.pageNum" :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="tableData.total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getTemplateList, deleteTemplate, updateStatus } from "@/api/tool";
import { getDictLabel } from "@/utils/dict";
export default {
  data() {
    return {
      cateOptions: [],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        name: "",
        enableStatus: "",
        platformType: "",
        tmplCate: ""
      },
      platformTypeList: [],
      statusList: [
        { label: "启用", value: 1 },
        { label: "禁用", value: 0 },
      ],
      tableData: {
        data: [],
        total: 0,
      },
      listLoading: false,
    };
  },
  async created() {
    this.cateOptions = await this.$getDictData("indus_tmpl_cate");
    this.platformTypeList = await this.$getDictData("platform_type");
    this.getList();
  },
  methods: {
    editStatus(v) {
      updateStatus(v.id).then((res) => {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.getList();
      });
    },
    deleteHunredQuestion(v) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteTemplate(v.id).then((res) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getList();
        });
      });
    },
    editHunredQuestion(v) {
      this.$router.push({
        path: "/tool/templateDataAdd",
        query: {
          id: v.id,
        },
      });
    },
    detail(id) {
      this.$router.push({
        path: "/tool/templateDataAdd",
        query: {
          id: id,
          type:'detail'
        },
      });
    },
    addHunredQuestion() {
      this.$router.push({
        path: "/tool/templateDataAdd",
      });
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getList();
    },
    getList() {
      getTemplateList(this.searchForm).then((res) => {
        res.data.list.forEach((item) => {
          item.platformName = getDictLabel(
            item.platformType,
            this.platformTypeList
          );
        });
        this.tableData.data = res.data.list;
        this.tableData.total = res.data.total;
        this.listLoading = false;
      });
    },
    search() {
      this.searchForm.pageNum = 1;
      this.getList();
    },
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        name: "",
        enableStatus: "",
        platformType: "",
      };
      this.getList();
    },
  },
};
</script>

<style></style>
